package com.controller.front;

import com.entity.Book;
import com.entity.User;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.service.BookService;
import com.util.JedisUtils;
import com.vo.Cart;
import com.vo.CartItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
@RequestMapping("front/cart")
public class CartController {
    @Autowired
    private BookService bookService;
    @RequestMapping("addCart")
    /*public String addCart(Integer id, HttpSession session){
        //先获取购物车对象
        Cart cart = (Cart)session.getAttribute("cart");
        //若购物车对象为空，则创建购物车对象
        if(cart == null){
            cart = new Cart();
            session.setAttribute("cart",cart);
        }
        //若不为空，则根据购物车对象获取到购物信息对象
        Map<Integer, CartItem> items = cart.getItems();
        //根据每条购物信息对象中的id属性判断是否已经添加过购物车
        //若之前添加过，则购买数量+1
        if(items.containsKey(id)){
            CartItem item = items.get(id);
            item.setCount(item.getCount()+1);
        }else{
            //若之前没有添加过，则新建购物信息对象，把购买数量设为1
            CartItem cartItem = new CartItem();
            cartItem.setCount(1);
            //根据id值获取到图书对象添加到购物信息中
            Book book = bookService.selectBookById(id);
            cartItem.setBook(book);
            items.put(id,cartItem);
        }
        return "forward:/front/cart/cart.jsp";
    }*/
    public String putCart(Integer id,HttpSession session) throws JsonProcessingException {
        //先获取购物车对象
        System.out.println("1");
        User user = (User)session.getAttribute("user");
        Jedis jedis = JedisUtils.getJedis();
        String jsonCart = jedis.get(user.getUser_id() + "");
        ObjectMapper mapper = new ObjectMapper();
        Cart cart = null;
        if(jsonCart != null){
            cart = mapper.readValue(jsonCart, Cart.class);
        }
        //若购物车对象为空，则创建购物车对象
        if(cart == null){
            cart = new Cart();
            session.setAttribute("cart",cart);
        }
        //若不为空，则根据购物车对象获取到购物信息对象
        Map<Integer, CartItem> items = cart.getItems();
        //根据每条购物信息对象中的id属性判断是否已经添加过购物车
        //若之前添加过，则购买数量+1
        if(items.containsKey(id)){
            CartItem item = items.get(id);
            item.setCount(item.getCount()+1);
        }else {
            CartItem cartItem = new CartItem();
            cartItem.setCount(1);
            //根据id值获取到图书对象添加到购物信息中
            Book book = bookService.selectBookById(id);
            cartItem.setBook(book);
            items.put(id, cartItem);
            String s = mapper.writeValueAsString(cart);
            jedis.set(user.getUser_id()+"", s);

        }
        JedisUtils.close(jedis);
        return "forward:/front/cart/showCart.do";
    }
    @RequestMapping("showCart")
    public String showCart(Model model,HttpSession session) throws JsonProcessingException {
        System.out.println("2");
        User user = (User)session.getAttribute("user");
        Jedis jedis = JedisUtils.getJedis();
        String jsonCart = jedis.get(user.getUser_id() + "");
        ObjectMapper mapper = new ObjectMapper();
        Cart cart = mapper.readValue(jsonCart, Cart.class);
        model.addAttribute(cart);
        JedisUtils.close(jedis);
        return "forward:/front/cart/cart.jsp";
    }
    @RequestMapping("increaseCart")
    public String increaseCart(Integer id,HttpSession session){
        Cart cart = (Cart)session.getAttribute("cart");
        Map<Integer, CartItem> items = cart.getItems();
        CartItem cartItem = items.get(id);
        cartItem.setCount(cartItem.getCount()+1);
        return "redirect:/front/cart/cart.jsp";
    }
    @RequestMapping("decreaseCart")
    public String decreaseCart(Integer id,HttpSession session){
        Cart cart = (Cart)session.getAttribute("cart");
        Map<Integer, CartItem> items = cart.getItems();
        CartItem cartItem = items.get(id);
        cartItem.setCount(cartItem.getCount()-1);
        if(cartItem.getCount() <= 0){
            items.remove(id);
        }
        return "redirect:/front/cart/cart.jsp";
    }
    @RequestMapping("removeCart")
    public String removeCart(Integer id,HttpSession session){
        Cart cart = (Cart)session.getAttribute("cart");
        Map<Integer, CartItem> items = cart.getItems();
        items.remove(id);
        return "redirect:/front/cart/cart.jsp";
    }

}
